﻿using System;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Moq;

namespace Zadatak3.Tests
{
    [TestClass]
    public class TermostatTest
    {
        Mock<Senzor> senzor;
        Mock<SignalListener> signalListener;
        Termostat termostat;

        [TestInitialize]
        public void InicijalizirajPrijeSvakogTesta()
        {
            senzor = new Mock<Senzor>();
            signalListener = new Mock<SignalListener>();
            termostat = new Termostat(signalListener.Object, senzor.Object);
        }

        [TestMethod]
        public void Termostat_inicijalno_ima_granicnu_temperaturu_podesenu_na_22()
        {
            Assert.AreEqual(22.0, termostat.GranicnaTemperatura);
        }

        [TestMethod]
        public void Termostatu_se_moze_mijenjati_granicna_temperatura()
        {
            termostat.GranicnaTemperatura = 1;
            Assert.AreEqual(1, termostat.GranicnaTemperatura);
            
            termostat.GranicnaTemperatura = 100;
            Assert.AreEqual(100, termostat.GranicnaTemperatura);
        }

        [TestMethod]
        public void Termostat_mora_dati_HIGH_signal_kada_temperatura_padne_ispod_granicne()
        {
            // Arrange
            senzor.Setup(s => s.DohvatiTrenutnuTemperaturu()).Returns(termostat.GranicnaTemperatura - 1);

            // Act
            termostat.provjeri();

            // Assert
            signalListener.Verify(sl => sl.OnSignal(true));
        }

        [TestMethod]
        public void Termostat_mora_dati_LOW_signal_kada_je_temperatura_jednaka_granicnoj()
        {
            // Arrange
            senzor.Setup(s => s.DohvatiTrenutnuTemperaturu()).Returns(termostat.GranicnaTemperatura);

            // Act
            termostat.provjeri();

            // Assert
            signalListener.Verify(sl => sl.OnSignal(false), Times.Once);
        }

        /// <summary>
        /// Primjer testne metode koja je potpuno neovisna - svi akteri se kreiraju unutar same metode.
        /// </summary>
        [TestMethod]
        public void Termostat_mora_dati_LOW_signal_kada_temperatura_predje_granicnu()
        {
            // Arrange
            Senzor senzor = Mock.Of<Senzor>(s => s.DohvatiTrenutnuTemperaturu() == termostat.GranicnaTemperatura+1);
            SignalListener signalListener = Mock.Of<SignalListener>();

            // Act
            new Termostat(signalListener, senzor).provjeri();

            // Assert
            Mock.Get(signalListener).Verify(sl => sl.OnSignal(false));
        }
    }
}
